﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;

namespace IHISCETeim.Web.MachineSys.FTradeModel
{
    public partial class FTradeReceivablesAdd : PageBase
    {
        protected override string DWZRef
        {
            get
            {
                return "FTradeReceivablesAdd";
            }
        }

        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.新增 };
            }
        }
        /// <summary>
        /// 是否是换机步骤2
        /// </summary>
        protected int IsHuanJi
        {
            get
            {
                return GetRequestValue<int>("IsHuanJi", 0);
            }
        }
        /// <summary>
        /// 换机步聚3所需参数,新的冲帐ID
        /// </summary>
        protected int NewFCatheringId
        {
            get
            {

                return GetRequestValue<int>("NewFCatheringId", 0);
            }
        }
        /// <summary>
        /// 换机步聚2所需参数,用于获取换机前的客户等信息
        /// </summary>
        protected int FTradeID
        {
            get
            {

                return GetRequestValue<int>("FTradeID", 0);
            }
        }


        #region 页面方法
        /// <summary>
        /// 获取所有销售类型SELECT　项
        /// </summary>
        /// <param name="XiaoShouLeiXing"></param>
        /// <returns></returns>
        protected string GetXiaoShouLeiXing(string XiaoShouLeiXing)
        {
            List<FTradeSaleTypeConfigInfo> list = FTradeSaleTypeConfigController.GetAll();
            StringBuilder sb = new StringBuilder();
            foreach (FTradeSaleTypeConfigInfo itm in list)
            {
                sb.AppendFormat("<option value=\"{0}\" {1}>{0}</option>",itm.XiaoShouLeiXing,itm.XiaoShouLeiXing==XiaoShouLeiXing?"selected":"");
            }

            return sb.ToString();
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                addmenu();
                
            }
        }
        private void addmenu()
        {
            FTradeReceivablesInfo objTrade = Request.SerializableObject<FTradeReceivablesInfo>();
            //objTrade.YueCuXiaoJinE = 0 - objTrade.YueCuXiaoJinE;

            FTradeSalesTypeInfo objSalesType = new FTradeSalesTypeInfo();
            objSalesType.XiaoShouLeiXing = objTrade.XiaoShouLeiXing;
            objSalesType.State = "正常";
            objSalesType.RealName = base.User.RealName;
            objSalesType.ExConfig = GetRequestValue<string>("SaleTypeConfig_Type", ""); //GetXiaoShouLeiXingExConfig(objTrade.XiaoShouLeiXing);


            List<FTradeAgeDetailInfo> listAgetDetails=new List<FTradeAgeDetailInfo>();

            char[] caSplit=new char[]{','};
            string[] Page_JinEs=GetRequestValue<string>("Page_JinE","").Split(caSplit);
            string[] Page_DaoQiRis=GetRequestValue<string>("Page_DaoQiRi","").Split(caSplit);
            string[] Page_ShowIndexs=GetRequestValue<string>("Page_ShowIndex","").Split(caSplit);
            string[] Page_TongJiNames=GetRequestValue<string>("Page_TongJiName","").Split(caSplit);
            string[] Page_ShowTitles=GetRequestValue<string>("Page_ShowTitle","").Split(caSplit);
            string[] Page_SuspendedMarks=GetRequestValue<string>("Page_SuspendedMark","").Split(caSplit);

            for(int index=0;index<Page_JinEs.Count();index++)
            {
                if (string.IsNullOrEmpty(Page_JinEs[index]) || Page_JinEs[index] == "0")
                    continue;

                FTradeAgeDetailInfo obj=new FTradeAgeDetailInfo();
                obj.ShowIndex = index;// Convert.ToInt32(Page_ShowIndexs[index]);
                obj.TongJiName=Page_TongJiNames[index];
                obj.ShowTitle=Page_ShowTitles[index];
                obj.JinE=Convert.ToDecimal(Page_JinEs[index]);
                obj.DaoQiRi=Convert.ToDateTime(Page_DaoQiRis[index]);
                obj.SuspendedMark=0;
                // obj.RealName="";
                obj.RealName=base.User.RealName;
                obj.YearRate = 0;
                obj.TanQeChenBen = 0;

                listAgetDetails.Add(obj);
                if (objTrade.XiaoShouLeiXing == "全款" || objTrade.XiaoShouLeiXing == "2个月帐期" || objTrade.XiaoShouLeiXing == "3个月帐期" || objTrade.XiaoShouLeiXing == "4个月及以上帐期" )
                {
                    objTrade.DaoQiRi = obj.DaoQiRi.ToString("yyyy-MM-dd");
                }
                else
                    objTrade.DaoQiRi = "";
            }

            string msg =FTradeReceivablesController.Add(objTrade, objSalesType, listAgetDetails);

            AddLog(Permission.新增, objTrade);

            if (msg == "")
            {
                //如果是换机,则进入 冲帐页面
                if (IsHuanJi == 1)
                {
                    //跳转参数
                    string succmsg = "已新增应收帐款成功,进入换机步骤3界面...";
                    string tabTitle = "换机步聚3(冲帐)";
                    string taburl = "FTradeModel/FTradeBalanceAdd.aspx?IsHuanJi=1&FCatheringId=" + NewFCatheringId.ToString()+"&JiHao="+HttpUtility.UrlEncode(objTrade.JiHao);
                    string tabrelId = "FTradeBalanceAdd";
                    SuccessOpenNavTab(succmsg, tabTitle, taburl, tabrelId);
                }

                SuccessJson("新增成功");
            }
            else
            {

                ErrorJson("新增失败<br/>" + msg);
            }
        }

        private string GetXiaoShouLeiXingExConfig(string XiaoShouLeiXing)
        {
            List<string> PageConfig = new List<string>();
            PageConfig.Add("PageConfig_QiShu");
            PageConfig.Add("PageConfig_QiShuType");
            PageConfig.Add("PageConfig_JinE");
            StringBuilder sb = new StringBuilder();
            switch(XiaoShouLeiXing.Trim())
            {
                case "全款":
                case "首付代垫":
                case "置换分期":
                    return "";
                    break;
                case "短期分期":
                case "置换加分期":
                    foreach (string itm in PageConfig)
                    {
                        sb.AppendFormat("{0}|{1}",itm,GetRequestValue<string>(itm,""));
                    }
                    return sb.ToString();
                     break;
                default:
                     return "";
                     break;
            }

            return "";
        }
    }
}